<template>
  <div>
    <!-- 文章标题 -->
    <el-form :rules="rules" ref="dataForm" :model="form" label-width="100px" style="height: 410px;">
    <el-form-item label="文章标题: " prop="title" >
      <el-input v-model="form.title" size="small"></el-input>
    </el-form-item>
    <!-- 内容 -->
    <el-form-item label="文章内容: "  prop='articleBody'>
        <quill-editor v-model="form.articleBody" :options="editorOption" />
    </el-form-item>
    <!-- 视频地址 -->
    <el-form-item  label="视频地址: " prop="videoURL" >
      <el-input v-model="form.videoURL" placeholder="请输入视频地址" size="small"></el-input>
    </el-form-item>
    <!-- 确定 取消 -->
    <el-form-item class="fr">
      <el-button v-if="cIsShowCancel" @click="$emit('close')">{{$t('table.cancel')}}</el-button>
      <el-button type="primary" @click="hAddorUpdate">{{$t('table.confirm')}}</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
import { detail, add, update } from '@/api/hmmm/articles'
export default {
  components: {
    quillEditor
  },
  props: ['value'],
  name: 'ArticlesAdd',
  data () {
    return {
      form: {
        title: '', // 标题
        articleBody: '', // 文章正文
        videoURL: '' // 视频地址
      },
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 3 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      // 表单验证
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleBody: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  // 创建完毕状态
  created () {
    console.log(this.value)// id
    if (this.value) {
      console.log(0) // 第一条数据
      this.hDetail()
    }
  },
  methods: {
    // 在编辑情况下,获取详情
    async hDetail () {
      const { value } = this
      if (!value) {
        return
      }
      this.form.id = value // 取得id
      const { data: res } = await detail({ id: value })
      console.log(res)
      // 获取详情 给form
      this.form = res
    },
    // 编辑
    async doUpdate () {
      const data = {
        ...this.form
      }
      console.log(data)
      await update(data)
      this.emit()
    },
    // 添加
    async doAdd () {
      try {
        await add({
          ...this.form
        })
        this.emit()
      } catch (err) {
        console.log(err)
      }
    },
    // 告诉父组件完成了
    emit () {
      // 事件监听
      this.$listeners.close && this.$emit('close')
      this.$listeners.refresh && this.$emit('refresh')
    },
    // 提交表单
    hAddorUpdate () {
      this.$message.success('提交成功!')
      this.$refs.dataForm.validate(valid => {
        // 趁早返回
        if (!valid) {
          this.$message.error('*号为必填项!')
          return
        }
        // 如果有value说明是编辑，做doUpdate
        this.value ? this.doUpdate() : this.doAdd()
      })
    }
  },
  computed: {
    cIsShowCancel () {
      return !!this.$listeners.close
    }
  }
}
</script>

<style scoped lang='scss'>
 ::v-deep .ql-editor {
 height: 200px;
 overflow: auto;
}
</style>
